package com.example.bailin.giftsay.guide;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.widget.ListView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.HomeCellEntity;
import com.example.bailin.giftsay.entity.LmSkipEntity;
import com.example.bailin.giftsay.guide.activity.NickNameActivity;
import com.example.bailin.giftsay.guide.activity.WebViewActivity;
import com.example.bailin.giftsay.guide.adapter.SelectionAdapter;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.sort.activity.LmSkipActivity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.MyListView;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/15.
 * <p/>
 * 指南里所有listview复用的Fragment
 */
public class GirlfriendFragment extends BaseFragment {
    private MyListView lv;
    private SelectionAdapter adapter;


    public static GirlfriendFragment getInstance(String url) {
        Bundle bundle = new Bundle();
        bundle.putString("key", url);
        GirlfriendFragment girlfriendFragment = new GirlfriendFragment();
        girlfriendFragment.setArguments(bundle);
        return girlfriendFragment;
    }

    @Override
    protected int setLayout() {
        return R.layout.fragment_mylistview;
    }

    @Override
    protected void initView() {
        lv = bindView(R.id.mylv_id);
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        String url = bundle.getString("key");
        tool().getData(url, HomeCellEntity.class, new NetTool.NetInterface<HomeCellEntity>() {
            @Override
            public void onSuccess(final HomeCellEntity homeCellEntity) {
                adapter = new SelectionAdapter();
                adapter.setHomeCellEntity(homeCellEntity);
                lv.setAdapter(adapter);
                adapter.setLmClickListener(new LmClickListener() {
                    @Override
                    public void onClick(int id, String title) {
                        Intent intent1 = new Intent(MyApp.getContext(), LmSkipActivity.class);
                        intent1.putExtra("id", id);
                        intent1.putExtra("title", title);
                        startActivity(intent1);
                    }
                });

                adapter.setPopupClick(new PopupClick() {
                    @Override
                    public void itemOnClick(int position) {
                        Intent intent = new Intent(MyApp.getContext(), WebViewActivity.class);
                        intent.putExtra("url", homeCellEntity.getData().getItems().get(position).getUrl());
                        startActivity(intent);
                    }
                });

                adapter.setNickNListener(new NickNListener() {
                    @Override
                    public void onClick(int id, String imgUrl, String name) {
                        Intent intent = new Intent(MyApp.getContext(), NickNameActivity.class);
                        intent.putExtra("imgUrl", imgUrl);
                        intent.putExtra("name", name);
                        intent.putExtra("id", id);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {
            }
        });


        lv.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new AsyncTask<Void, Void, Void>(){

                    @Override
                    protected Void doInBackground(Void... params) {

                        try {
                            Thread.sleep(1800);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        adapter.notifyDataSetChanged();
                        lv.onRefreshComplete();
                    }
                }.execute(null, null, null);
            }
        });
    }
}
